<?php
ini_set('MEMORY_LIMIT', '1000M');
require_once 'common.php';

$file_list = get_file_list("../wallbase/images");

for ($i=0; $i<1000; $i++)
	echo auto_add_item($file_list)."\n";

function auto_add_item($file_list)
{
	$item_image_original = array_mt_rand($file_list);
	$item_image_name = $file_list[$item_image_original];
	if (!file_exists($item_image_original) || !filesize($item_image_original)
		|| filesize($item_image_original)>500000) return;
	
	$item_x = mt_rand(0, 499);
	$item_y = mt_rand(0, 499);
	$item_id = sys::sql("insert into test_items (item_image, item_x, item_y)"
		." values ('%s', '%d', '%d') on duplicate key update item_image='%s';",
		$item_image_name, $item_x, $item_y, $item_image_name);
	itemimage::save($item_id, $item_image_original);
	
	return $item_id;
}

function get_file_list($dir)
{
	$ret_arr = array();
	
	if ($handle = opendir($dir)) {
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != "..") {
				$ret_arr["$dir/$file"] = $file;
			}
		}
		closedir($handle);
	}
	
	return $ret_arr;
}

function array_mt_rand(array $array, $numberOfKeys = 1)
{
	if (!is_int($numberOfKeys)) throw new Exception;
	if ($numberOfKeys < 1) throw new Exception;
	$keys = array_keys($array);
	$maximum = count($array) - 1;
	if ($numberOfKeys == 1) {
	return $keys[mt_rand(0, $maximum)];
	} else {
	$randomKeys = array();
	for ($i = 0; $i < $numberOfKeys; $i++) {
	$randomKeys[] = $keys[mt_rand(0, $maximum)];
	}
	return $randomKeys;
	}
}